namespace EyeSoft.ServiceLocator.Windsor
{
	using System;

	using Castle.MicroKernel.Registration;
	using Castle.Windsor;

	using Microsoft.Practices.ServiceLocation;

	public abstract class WindsorContainerRegister
	{
		protected readonly IWindsorContainer kernel = new WindsorContainer();
		private readonly IServiceLocator locator;

		protected WindsorContainerRegister()
		{
			locator = new WindsorServiceLocator(kernel);

			RegisterAll();
		}

		public IServiceLocator Locator
		{
			get
			{
				return locator;
			}
		}

		protected abstract void RegisterAll();

		protected virtual void RegisterDeferred<TContract>(Func<TContract> implementation)
			where TContract : class
		{
			Register(implementation);
		}

		protected virtual void Register<TContract>(TContract implementation)
			where TContract : class
		{
			kernel.Register(Component.For<TContract>().Instance(implementation));
		}
	}
}